package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbBangfuMapper;
import com.sinog.biz.mapper.havexml.AbGenzongbangjiaoMapper;
import com.sinog.biz.mapper.havexml.AbPaichaMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.AbShehuijiuzhuMapper;
import com.sinog.biz.mapper.havexml.AbZyzjiuzhuMapper;
import com.sinog.biz.mapper.havexml.SocialOrganizeInfoMapper;
import com.sinog.biz.mapper.havexml.SocialTrainingMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjgzBangfuInfoMainService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbBangfuResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 帮教工作信息管理 - 帮教管理
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzBangfuInfoMainServiceImpl extends ServiceImpl<AbBangfuMapper,AbBangfuResultVo> implements BjgzBangfuInfoMainService {

    @Resource
    private AbBangfuMapper abBangfuMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;
    @Resource
    private SocialOrganizeInfoMapper socialOrganizeInfoMapper;
    @Resource
    private AbGenzongbangjiaoMapper abGenzongbangjiaoMapper;
    @Resource
    private AbShehuijiuzhuMapper abShehuijiuzhuMapper;
    @Resource
    private AbZyzjiuzhuMapper abZyzjiuzhuMapper;
    @Resource
    private AbPaichaMapper abPaichaMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;
    @Resource
    private SocialTrainingMapper socialTrainingMapper;

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 帮教 - 保存
     * @param dataStr 业务数据
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveBjInfo(String dataStr,UserDTO userDto) {
        Date date = new Date();
        //参数实体
        AbBangfuResultVo abBangfuResultVo = JsonUtil.toBean(dataStr,AbBangfuResultVo.class);
        //新增
        if(StringUtils.isBlank(abBangfuResultVo.getId())) {
            abBangfuResultVo.setId(IdGen.strid());
            abBangfuResultVo.setCreateUser(userDto.getUsername());
            abBangfuResultVo.setCreateTime(date);
            abBangfuResultVo.setAdduserid(userDto.getDepartid());
        }
        abBangfuResultVo.setUpdateUser(userDto.getUsername());
        abBangfuResultVo.setUpdateTime(date);
        boolean tag = abBangfuResultVo.insertOrUpdate();
        if(tag) {
            //更新帮扶状态
            abPrisonerMapper.updateBjztExistsByPriNumBf(abBangfuResultVo.getPriNum());
            return Result.success(abBangfuResultVo.getId());
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取帮教信息
     * @param pp 帮教对象标志位
     * @return Result
     */
    @Override
    public Result getBjBaseInfo(String pp) {
        Map<String,Object> map = abBangfuMapper.getBjBaseInfo(pp);
        List<Map<String,Object>> appendixBjList = uploadAppendixFileMapper.getUploadAppendixFile(pp,null,"14");
        if(null != map && !map.isEmpty()) {
            map.put("appendixBjList",appendixBjList);
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 帮教 - 列表
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getBjInfoList(String priNum) {
        //本系统的本地code
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        List<Map<String,Object>> list = abBangfuMapper.getBjInfoList(priNum,flag);
        List<Map<String,Object>> reList = new ArrayList<>(16);
        for(Map<String,Object> map : list) {
            reList.add(StrUtil.mapToString(map));
        }
        return reList;
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 帮教 - 删除
     * @param pp 帮教对象标志位
     * @param priNum 服刑人员编号
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delBjInfoAction(String pp,String priNum) {
        int num = abBangfuMapper.delBjInfoAction(pp);
        //删除党政
        abGenzongbangjiaoMapper.delBjDzInfoAction(pp);
        //删除社会组织
        abShehuijiuzhuMapper.delBjShzzInfoAction(pp);
        //删除志愿者
        abZyzjiuzhuMapper.delBjZyzInfoAction(pp);
        //删除排查
        abPaichaMapper.delBjPcInfoAction(pp);
        if(0 < num) {
            //更新帮扶状态
            abPrisonerMapper.updateBjztByPriNumBf(priNum);
            return Result.success("删除成功");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 获取帮扶社会组织列表
     * @param zzType zzType
     * @param user 当前登录用户
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getShgyzzInfoList(String zzType,UserDTO user) {
        return StrUtil.listToString(socialOrganizeInfoMapper.getShgyzzInfoList(zzType,user.getDepartid()));
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 帮教 - 就业信息列表
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getBjJyxxInfoList(String priNum) {
        return abPrisonerMapper.getBjJyxxInfoList(priNum);
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 帮教 - 培训信息列表
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getBjPxjgInfoList(String priNum) {
        return socialTrainingMapper.getBjPxjgInfoList(priNum);
    }

    /**
     * 获取帮教--培训机构
     * @param priNum 服刑人员编号
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getBjZzcyInfoList(String priNum) {
        return abPrisonerMapper.getBjZzcyInfoList(priNum);
    }
}